
const { regeneratorRuntime } = global;

import { apiActivityLine } from '../../../other/api';

Page(global.connect(
  state => ({
    ...state.globalReducer,
    ...state.activityReducer
  }),
  dispatch => ({
    handleRoute: global.handleRoute,
    async getActivityDetail(data) {
      return await dispatch(global.activityReducer.getActivityDetail(data))
    },
    async getActivityOrderList(data) {
      return await dispatch(global.activityReducer.getActivityOrderList(data))
    },
    // 确定开发下单权限
    async openStore(data) {
      return await dispatch(global.activityReducer.openStore(data))
    },
    // 删除活动
    async deleteActivity(data) {
      return await dispatch(global.activityReducer.deleteActivity(data))
    },
    // 准备预览图数据
    async prepareEffectImage(data) {
      return await dispatch(global.globalReducer.prepareEffectImage(data))
    },
  })
)({
  data: {
    uiTabIndex: 0,
    uiShowSearch: false,
    uiMessage: '',
    uiPline: [
      // { name: '未下单', num: 10 },
      // { name: '待审核', num: 5 },
      // { name: '未通过', num: 2 },
      // { name: '已通过', num: 0 },
      // { name: '待报价', num: 0 },
      // { name: '生成完成', num: 0 },
      // { name: '已接收', num: 0 },
      // { name: '待接收任务', num: 0 },
      // { name: '待确定报价', num: 0 },
      // { name: '待上画', num: 0 },
      // { name: '上画进行中', num: 0 },
      // { name: '上画待验收', num: 0 },
      // { name: '待下画', num: 0 },
      // { name: '下画进行中', num: 0 },
      // { name: '下画待验收', num: 0 },
      // { name: '完成', num: 0 },
      // { name: '全部', num: 0 }
    ],
    stores: {
      list: [
        { storeName: '一个假的店铺' }
      ]
    },
    activeArr: [],
    keyword: '',
    i: 3,
    j: 0
  },
  onLoad(options) {
    console.log('activity detail onLoad')
    const { activity_id } = options
    this.setData({ activity_id })

    // this.handleSearch();
    return;

    apiActivityLine({ id: activity_id }).then(res => {
      if (res.apiStatus === 'SUCCESS') {
        res.data.map(i => i.active = false)
        let line1 = res.data.slice(0, 5)
        let line2 = res.data.slice(5, 9).reverse()
        let line3 = res.data.slice(9, 13)
        let line4 = res.data.slice(13).reverse()
        line4.unshift({
          active: true,
          statusMsg: '全部'
        })
        // line2.reverse()
        // line4.reverse()
        console.log(line1);
        console.log(line2);
        console.log(line3);
        console.log(line4);

        // console.log([...line1, ...line2, ...line3, ...line4])
        this.setData({
          uiPline: [line1, line2, line3, line4]
        })
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '获取活动信息失败'
        })
      }
    }).catch(err => {
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || '获取活动信息失败'
      })
    })
  },
  onShow() {
    this.handleSearch();

    const { activity_id } = this.data
    this.setData({ activity_id })
    this.getActivityDetail({ activity_id })
    this.getActivityOrderList({ activity_id })
  },
  handlePlineItem(e) {
    const { i, j } = e.currentTarget.dataset;
    const { uiPline } = this.data;
    uiPline.map(i => i.map(j => j.active = false));
    uiPline[i][j].active = true;
    this.setData({ i, j, uiPline, activeArr: uiPline[i][j].data });
  },
  handleInput(e) {
    console.log('handleInput', e)
    const keyword = e.detail.detail.value;
    this.setData({ keyword });
    this.handleSearch()
  },
  handleSearch() {
    const { keyword, activity_id } = this.data;

    apiActivityLine({ id: activity_id, keyword }).then(res => {
      if (res.apiStatus === 'SUCCESS') {
        res.data.map(i => i.active = false)
        let line1 = res.data.slice(0, 5)
        let line2 = res.data.slice(5, 9).reverse()
        let line3 = res.data.slice(9, 13)
        let line4 = res.data.slice(13).reverse()
        let arr = []
        res.data.map(i => {
          arr = [...arr, ...i.data]
        })
        line4.unshift({
          active: false,
          statusMsg: '全部',
          data: arr
        })
        console.log(line1);
        console.log(line2);
        console.log(line3);
        console.log(line4);
        const uiPline = [line1, line2, line3, line4];
        const { i, j } = this.data;
        const activeArr = uiPline[i][j].data;
        uiPline[i][j].active = true;
        this.setData({ activeArr, uiPline });
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '获取活动信息失败'
        })
      }
    }).catch(err => {
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || '获取活动信息失败'
      })
    })
  },
  // 取消 开放权限
  handleDialog1Cancel(e) {
    this.selectComponent('#dialog1').handleHide()
  },
  // 取消 删除活动
  handleDialog2Cancel(e) {
    this.selectComponent('#dialog2').handleHide()
  },
  // 确认 开放下单权限
  handleDialog1Confirm(e) {
    this.handleDialog1Cancel()
    const formData = {
      activity_id: 'x',
      store_id: 'x'
    }
    this.openStore(formData).then(res => {
      console.log(res)
      if (res.actionStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: '开放成功'
        })
        global.handleRoute({ route: 'index' })
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '开放失败'
        })
      }
    }).catch(err => {
      console.error(err)
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || '开放失败'
      })
    })
  },
  // 确认 删除活动
  handleDialog2Confirm(e) {
    this.handleDialog2Cancel()
    const { activity_id } = this.data;
    const formData = {
      activity_id
    }
    this.deleteActivity(formData).then(res => {
      console.log(res)
      if (res.actionStatus === 'SUCCESS') {
        this.selectComponent('#toast').handleShow({
          type: 'success',
          message: '删除活动成功'
        })
        global.handleRoute({ route: 'index' })
      } else {
        this.selectComponent('#toast').handleShow({
          message: res.apiMes || '删除活动失败'
        })
      }
    }).catch(err => {
      console.error(err)
      this.selectComponent('#toast').handleShow({
        message: err && err.apiMes || '删除活动失败'
      })
    })
  },
  // 返回上一页
  handleLicon1Tap(e) {
    wx.navigateBack()
  },
  // 展开|收起 搜索框
  handleRicon1Tap(e) {
    this.setData({
      uiShowSearch: !this.data.uiShowSearch
    })
  },
  // 查看活动效果图
  handleRicon2Tap(e) {
    console.log('handleRicon2Tap', e)
    // todo
    const urls = (this.data.activityDetail.data.imgs || []).map(i => {
      return {
        name: 'x',
        url: 'y'
      }
    })
    this.prepareEffectImage(urls)
    global.handleRoute({ route: 'effect' })
  },
  // 删除活动
  handleRicon3Tap(e) {
    this.setData({
      uiMessage: `确定删除${this.data.activityDetail.data.name}？`
    })
    this.selectComponent('#dialog2').handleShow()
  },
  handleTabTap(e) {
    this.setData({
      uiTabIndex: e.currentTarget.dataset.index
    })
  },
  handleOpenItem(e) {
    this.setData({
      uiMessage: `确定为下单超时${this.data.orderDetail.data.activity_name}开放下单授权？`
    })
    this.selectComponent('#dialog1').handleShow()
  },
  handleStoreItemTap(e) {
    console.log('handleStoreItemTap', e)
    const { activity_id, order_no, order_status } = e.currentTarget.dataset.detail
    if (order_status != '未下单') {
      this.handleRoute({ route: 'order-detail', query: { activity_id, order_no } })
    }
  }
}))
